﻿using System;
using System.Collections.Generic;
using System.Linq;

public class Order
{
    public int OrderId { get; set; }
    public DateTime OrderDate { get; set; }
    public Customer Customer { get; set; }
    public List<OrderDetails> OrderDetails { get; set; }

    public double TotalAmount
    {
        get
        {
            double total = 0;
            foreach (var item in OrderDetails)
            {
                total += item.Quantity * item.Product.Price;
            }
            return total;
        }
    }

    public Order(int orderId, DateTime orderDate, Customer customer)
    {
        OrderId = orderId;
        OrderDate = orderDate;
        Customer = customer;
        OrderDetails = new List<OrderDetails>();
    }

    // 重写Equals方法，确保添加的订单不重复
    public override bool Equals(object obj)
    {
        if (obj is Order order)
        {
            return this.OrderId == order.OrderId;
        }
        return false;
    }

    public override int GetHashCode()
    {
        return OrderId.GetHashCode();
    }

    // 重写ToString方法来显示订单信息
    public override string ToString()
    {
        return $"订单号: {OrderId}, 客户: {Customer.Name}, 总金额: {TotalAmount:C}";
    }
}

public class OrderDetails
{
    public Product Product { get; set; }
    public int Quantity { get; set; }

    public OrderDetails(Product product, int quantity)
    {
        Product = product;
        Quantity = quantity;
    }

    // 重写Equals方法，确保每个订单的订单明细不重复
    public override bool Equals(object obj)
    {
        if (obj is OrderDetails details)
        {
            return this.Product.ProductId == details.Product.ProductId;
        }
        return false;
    }

    public override int GetHashCode()
    {
        return Product.ProductId.GetHashCode();
    }

    // 重写ToString方法来显示订单明细信息
    public override string ToString()
    {
        return $"{Product.Name}, 数量: {Quantity}, 单价: {Product.Price:C}";
    }
}

public class Customer
{
    public string Name { get; set; }

    public Customer(string name)
    {
        Name = name;
    }

    public override string ToString()
    {
        return Name;
    }
}

public class Product
{
    public int ProductId { get; set; }
    public string Name { get; set; }
    public double Price { get; set; }

    public Product(int productId, string name, double price)
    {
        ProductId = productId;
        Name = name;
        Price = price;
    }
}

public class OrderService
{
    private List<Order> _orders;

    public OrderService()
    {
        _orders = new List<Order>();
    }

    // 添加订单
    public void AddOrder(Order order)
    {
        if (_orders.Contains(order))
        {
            throw new InvalidOperationException("该订单已经存在！");
        }
        _orders.Add(order);
    }

    // 删除订单
    public void RemoveOrder(int orderId)
    {
        var order = _orders.FirstOrDefault(o => o.OrderId == orderId);
        if (order == null)
        {
            throw new InvalidOperationException("找不到该订单！");
        }
        _orders.Remove(order);
    }

    // 修改订单
    public void UpdateOrder(int orderId, DateTime newDate, Customer newCustomer)
    {
        var order = _orders.FirstOrDefault(o => o.OrderId == orderId);
        if (order == null)
        {
            throw new InvalidOperationException("找不到该订单！");
        }
        order.OrderDate = newDate;
        order.Customer = newCustomer;
    }

    // 查询订单
    public List<Order> QueryOrders(Func<Order, bool> predicate)
    {
        var result = _orders.Where(predicate).OrderBy(o => o.TotalAmount).ToList();
        return result;
    }

    // 排序方法，默认按照订单号排序
    public void SortOrders(Func<Order, object> keySelector = null)
    {
        keySelector ??= o => o.OrderId;
        _orders = _orders.OrderBy(keySelector).ToList();
    }

    // 显示所有订单
    public void DisplayOrders()
    {
        foreach (var order in _orders)
        {
            Console.WriteLine(order);
            foreach (var detail in order.OrderDetails)
            {
                Console.WriteLine("    " + detail);
            }
        }
    }
}

public class Program
{
    public static void Main(string[] args)
    {
        try
        {
            // 创建产品
            Product product1 = new Product(1, "苹果", 5);
            Product product2 = new Product(2, "香蕉", 3);

            // 创建客户
            Customer customer = new Customer("张三");

            // 创建订单
            Order order1 = new Order(101, DateTime.Now, customer);
            order1.OrderDetails.Add(new OrderDetails(product1, 2));
            order1.OrderDetails.Add(new OrderDetails(product2, 3));

            Order order2 = new Order(102, DateTime.Now, customer);
            order2.OrderDetails.Add(new OrderDetails(product1, 1));
            order2.OrderDetails.Add(new OrderDetails(product2, 5));

            // 初始化订单服务
            OrderService orderService = new OrderService();

            // 添加订单
            orderService.AddOrder(order1);
            orderService.AddOrder(order2);

            // 显示所有订单
            Console.WriteLine("所有订单:");
            orderService.DisplayOrders();

            // 查询订单
            Console.WriteLine("\n查询金额大于15元的订单:");
            var queriedOrders = orderService.QueryOrders(o => o.TotalAmount > 15);
            foreach (var order in queriedOrders)
            {
                Console.WriteLine(order);
            }

            // 删除订单
            orderService.RemoveOrder(101);
            Console.WriteLine("\n删除订单101后的订单:");
            orderService.DisplayOrders();

            // 修改订单
            orderService.UpdateOrder(102, DateTime.Now.AddDays(1), new Customer("李四"));
            Console.WriteLine("\n修改订单102后的订单:");
            orderService.DisplayOrders();

            // 排序订单（根据总金额降序排序）
            orderService.SortOrders(o => o.TotalAmount);
            Console.WriteLine("\n按订单金额降序排序后的订单:");
            orderService.DisplayOrders();
        }
        catch (Exception ex)
        {
            Console.WriteLine("错误: " + ex.Message);
        }
    }
}